local sk__huituo = fk.CreateSkill {

  name = "sk__huituo",

  tags = {  },

}



sk__huituo:addEffect(fk.TurnStart, {
  name = "sk__huituo",
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(sk__huituo.name) and #player:getTableMark(sk__huituo.name) < 4
  end,
  on_cost = function (self, event, target, player, data)
    local _, dat = player.room:askForUseActiveSkill(player, "sk__huituo_active", "#sk__huituo-invoke", true, nil, false)
    if dat then
      event:setCostData(self, dat)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(event:getCostData(self).targets[1])
    local choice = event:getCostData(self).interaction
    local mark = player:getTableMark(sk__huituo.name)
    table.insert(mark, tonumber(choice[11]))
    room:setPlayerMark(player, sk__huituo.name, mark)
    if choice == "sk__huituo1" then
      if not to:isWounded() then return end
      local n = to.hp
      for _, p in ipairs(room.alive_players) do
        if p.hp > n then
          n = p.hp
        end
      end
      n = n + 1
      room:recover{
        who = to,
        num = math.min(n - to.hp, to:getLostHp()),
        recoverBy = player,
        skillName = sk__huituo.name,
      }
    elseif choice == "sk__huituo2" then
      local n = to:getHandcardNum()
      for _, p in ipairs(room.alive_players) do
        if p:getHandcardNum() > n then
          n = p:getHandcardNum()
        end
      end
      n = n + 1
      to:drawCards(n - to:getHandcardNum(), sk__huituo.name)
    elseif choice == "sk__huituo3" then
      for _, slot in ipairs(to:getAvailableEquipSlots()) do
        if to.dead then return end
        local type = Util.convertSubtypeAndEquipSlot(slot)
        if #to:getEquipments(type) < #to:getAvailableEquipSlots(type) then
          local cards = table.filter(room.draw_pile, function(id)
            local card = Fk:getCardById(id)
            return card.sub_type == type and not to:isProhibited(to, card)
          end)
          table.insertTable(cards, table.filter(room.discard_pile, function(id)
            local card = Fk:getCardById(id)
            return card.sub_type == type and not to:isProhibited(to, card)
          end))
          if #cards > 0 then
            room:useCard({
              from = to,
              tos = {to},
              card = Fk:getCardById(table.random(cards)),
            })
          end
        end
      end
    elseif choice == "sk__huituo4" then
      room:handleAddLoseSkills(to, sk__huituo.name, nil, true, false)
    end
  end,
})

return sk__huituo